import { lazy } from 'react';
import { createBrowserRouter, Outlet, Navigate } from 'react-router-dom';
import BaseLayout from '../layouts/BaseLayout';
import AuthLayout from '../layouts/AuthLayout';
import AuthGuard from '../components/AuthGuard';
import AdminGuard from './AdminGuard';
import AdminLayout from '@/layouts/AdminLayout';
import HomePage from '../pages/home';
import NotFound from '../pages/error/404';
import Forbidden from '../pages/error/403';
import ServerError from '../pages/error/500';
import LoginPage from '../pages/auth/login';
import RegisterPage from '../pages/auth/register';
import Profile from '../pages/user/profile';

// 懒加载路由组件
const DashboardPage = lazy(() => import('@/pages/admin/dashboard'));
const UsersPage = lazy(() => import('@/pages/admin/users'));
const MedicineList = lazy(() => import('@/pages/admin/medicines/medicine'));
const CategoryList = lazy(() => import('@/pages/admin/medicines/category'));
const DiseaseCategoryList = lazy(() => import('@/pages/admin/disease/category'));
const DiseaseList = lazy(() => import('@/pages/admin/disease/list'));
const AdminOrderList = lazy(() => import('@/pages/admin/orders'));
const AdminOrderDetail = lazy(() => import('@/pages/admin/orders/detail'));
const AdminProfile = lazy(() => import('@/pages/admin/profile'));
const FeedbackList = lazy(() => import('@/pages/admin/feedbacks'));
const ConsultationList = lazy(() => import('@/pages/admin/consultations'));

// 前台疾病页面
const PublicDiseaseList = lazy(() => import('@/pages/disease/list'));
const PublicDiseaseDetail = lazy(() => import('@/pages/disease/detail'));

// 前台药品页面
const PublicMedicineList = lazy(() => import('@/pages/medicine/list'));
const PublicMedicineDetail = lazy(() => import('@/pages/medicine/detail'));

// 购物车和订单页面
const Cart = lazy(() => import('@/pages/cart'));
const OrderList = lazy(() => import('@/pages/order/list'));
const OrderDetail = lazy(() => import('@/pages/order/detail'));

// AI问诊页面
const ConsultationChat = lazy(() => import('@/pages/ai/consultation'));
const ConsultationHistory = lazy(() => import('@/pages/ai/consultation/history'));

// 路由配置
const router = createBrowserRouter([
  {
    path: '/',
    element: <BaseLayout />,
    children: [
      { index: true, element: <HomePage /> },
      { path: 'disease/list', element: <PublicDiseaseList /> },
      { path: 'disease/detail/:id', element: <PublicDiseaseDetail /> },
      { path: 'medicine/list', element: <PublicMedicineList /> },
      { path: 'medicine/detail/:id', element: <PublicMedicineDetail /> },
      {
        path: 'ai/consultation',
        element: (
          <AuthGuard>
            <Outlet />
          </AuthGuard>
        ),
        children: [
          { index: true, element: <ConsultationChat /> },
          { path: 'history', element: <ConsultationHistory /> },
        ],
      },
      {
        path: 'user',
        element: (
          <AuthGuard>
            <Outlet />
          </AuthGuard>
        ),
        children: [
          { path: 'profile', element: <Profile /> },
          { path: 'cart', element: <Cart /> },
          { path: 'order/list', element: <OrderList /> },
          { path: 'order/detail/:orderNo', element: <OrderDetail /> },
        ],
      },
    ],
  },
  {
    path: '/auth',
    element: <AuthLayout />,
    children: [
      { path: 'login', element: <LoginPage /> },
      { path: 'register', element: <RegisterPage /> },
    ],
  },
  // 管理后台路由
  {
    path: '/admin',
    element: (
      <AuthGuard>
        <AdminGuard>
          <AdminLayout />
        </AdminGuard>
      </AuthGuard>
    ),
    children: [
      { index: true, element: <Navigate to="/admin/dashboard" replace /> },
      { path: 'dashboard', element: <DashboardPage /> },
      { path: 'users', element: <UsersPage /> },
      { path: 'medicines', element: <MedicineList /> },
      { path: 'medicines/categories', element: <CategoryList /> },
      { path: 'disease/categories', element: <DiseaseCategoryList /> },
      { path: 'diseases', element: <DiseaseList /> },
      { path: 'orders', element: <AdminOrderList /> },
      { path: 'orders/detail/:orderNo', element: <AdminOrderDetail /> },
      { path: 'profile', element: <AdminProfile /> },
      { path: 'feedbacks', element: <FeedbackList /> },
      { path: 'consultations', element: <ConsultationList /> },
    ],
  },
  // 错误页面路由
  {
    path: '/403',
    element: <Forbidden />,
  },
  {
    path: '/500',
    element: <ServerError />,
  },
  {
    path: '*',
    element: <NotFound />,
  },
]);

export default router; 